﻿using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using DevelopersToolbox;

namespace XmlTools {
    public partial class XmlFormatControl : UserControl, IExecutableTool {
        public XmlFormatControl() {
            InitializeComponent();
        }

        private void _formatXmlButton_Click(object sender, EventArgs e) {
            Execute();
        }

        public void Execute() {
            try {
                XmlDocument doc = _sourceXml.XmlDocument;
                StringBuilder sb = new StringBuilder();
                using (TextWriter textWriter = new StringWriter(sb)) {
                    XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
                    xmlWriterSettings.Indent = true;
                    using (XmlWriter xmlWriter = XmlWriter.Create(textWriter, xmlWriterSettings)) {
                        doc.WriteTo(xmlWriter);
                    }
                }
                _formattedXml.Text = sb.ToString();
            }
            catch (Exception ex) {
                _formattedXml.Text = ex.ToString();
            }
        }
    }
}